package com.qdh.demo5;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

// CV WordCountJob 局部修改
//用来：测试split个数
public class Combine11Job {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        // 1. 初始化配置
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop11:8020");
        //2. 创建job
        Job job = Job.getInstance(conf);
        job.setJarByClass(Combine11Job.class);

        //3. 设置输入格式化工具和输出格式化
        //job.setInputFormatClass(TextInputFormat.class);
        //job.setOutputFormatClass(TextOutputFormat.class);

        job.setInputFormatClass(CombineTextInputFormat.class);
        CombineTextInputFormat.setMinInputSplitSize(job, 10485760);//最小分片大小 10M
        CombineTextInputFormat.setMaxInputSplitSize(job,134217728);//最大分片大小 128M
        CombineTextInputFormat.addInputPath(job,new Path("/mapreduce/demo7/"));

        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,new Path("/mapreduce/demo7/out333"));

        //5. 设置mapper和reducer
        job.setMapperClass(Combine11Mapper.class);
        job.setReducerClass(Combine11Reducer.class);

        // 6. 设置mapper的kv类型和reducer的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //设置合并类：让mapReduce预先执行 Reducer中的reduce业务代码
        //减轻reduce压力
        job.setCombinerClass(Combine11Reducer.class);

        // 7. 启动job
        boolean b = job.waitForCompletion(true);
        System.out.println(b?"成功":"失败");
    }
    //局部计算
    static  class Combine11Mapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] split = value.toString().split(" ");//"张三 李四 小王 张三"
            for (String s : split) {
                context.write(new Text(s.trim()), new IntWritable(1));
            }
        }
    }
    //汇总计算
    static class Combine11Reducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            // value 张三 [1,1,1,1,1,1,1]
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }
}
